package cn.cuiqi.dynamic.thread.pool.sdk.trigger.job;

import cn.cuiqi.dynamic.thread.pool.sdk.domain.IDynamicThreadPoolService;
import cn.cuiqi.dynamic.thread.pool.sdk.domain.model.Entity.ThreadPoolConfigEntity;
import cn.cuiqi.dynamic.thread.pool.sdk.registry.IRegistry;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Cuiqi
 * @Description:
 * @Date: 2024/8/1 21:39
 **/
@Service
public class ThreadPoolReportJob {
    
    private final Logger logger = LoggerFactory.getLogger(ThreadPoolReportJob.class);   
    @Autowired
    private  IDynamicThreadPoolService dynamicThreadPoolService;
    @Autowired
    private  IRegistry registry;

   

    @Scheduled(cron = "0/30 * * * * ?")
    public void reprotThreadPool() {
        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
        registry.reportThreadPoolList(threadPoolConfigEntities);
        logger.info("动态线程池，上报线程池：{}", JSON.toJSONString(threadPoolConfigEntities));

        if (threadPoolConfigEntities != null && !threadPoolConfigEntities.isEmpty()) {
            for (ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigEntities) {
                registry.reportThreadPoolParamter(threadPoolConfigEntity);
                logger.info("动态线程池，上报线程池：{}", JSON.toJSONString(threadPoolConfigEntity));
            }
        }
        
    }
    
}
